<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像模块 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt />
      </div>
      <!-- 表单 -->
      <el-form class="form_login" :model="LoginForm" :rules="LoginFormRules" ref="ruleForm">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-user" v-model="LoginForm.username"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input prefix-icon="iconfont icon-3702mima" v-model="LoginForm.password"></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="Login">登录</el-button>
          <el-button type="info" @click="LoginFormReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 表单的双向绑定
      LoginForm: {
        username: "admin",
        password: "123456"
      },
      // 表单的验证规则
      LoginFormRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入用户密码", trigger: "blur" },
          { min: 6, max: 15, message: "长度在 6 到 15 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    LoginFormReset() {
      // console.log(this);
      this.$refs.ruleForm.resetFields();
    },
    Login() {
      // 在提交登录请求前，先验证表单
      this.$refs.ruleForm.validate(async valid => {
        if (!valid) return;
        // console.log(valid)
        // 验证成功发起请求
        // { data: res }:解构  给data重新赋值：res
        const { data: res } = await this.$http.post("login", this.LoginForm);
        // console.log(res);
        if (res.meta.status != 200) return this.$message.error(res.meta.msg);
        this.$message.success(res.meta.msg);
        // 登录成功生成token 将生成的 token 保存到 sessionStorage 本地存储
        window.sessionStorage.setItem('token',res.data.token);
        // 登录成功跳转到首页
        this.$router.push('/home');
      });
    }
  }
};
</script>

<style lang='less' scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatar_box {
    padding: 10px;
    width: 130px;
    height: 130px;
    background-color: #fff;
    border-radius: 50%;
    border: 1px solid #ddd;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      border: 1px solid #ddd;
      box-shadow: 0 0 15px #ddd;
      background-color: #eee;
    }
  }
  .form_login {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
    .btns {
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>